<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像 -->
      <div class="avatar-box">
        <img src="../assets/imgs/login_logo.png">
      </div>
      <!-- 标题 -->
      <div class="title">
        电商后台管理系统
      </div>
      <!-- 登录表单 -->
      <el-form ref="loginFormRefs" class="login-form" :model="loginForm" :rules="loginFormRules">
        <el-form-item prop="username">
          <el-input placeholder="请输入账号" prefix-icon="el-icon-user-solid" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" prefix-icon="el-icon-key" show-password v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-btn" @click="login">确认</el-button>
        </el-form-item>
      </el-form>
    </div>  
  </div>
</template>

<script>
export default {
  data(){
    return {
      // 表单的数据绑定对象
      loginForm:{ 
        username:'',
        password:''
      },
      // 表单的校验规则
      loginFormRules: {
        username: [
          { required: true, message: '请输入登录账户', trigger: 'blur' },
          { min: 3, max: 10, message: '账户名为3~10个字符组成', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '密码为6~15个字符组成', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login() {
      // 判断是否校验成功
      this.$refs.loginFormRefs.validate(async valid =>{
        if(!valid){
          return
        }
        // 发送请求判断是否登录成功
        // {data:res}得到返回数据中的data命名为res
        const {data:res} = await this.$http.post('login',this.loginForm)
        if(res.meta.status !== 200){
          return this.$message.error('登录失败');
        }
    
        this.$message.success('登录成功');
        // 将用户信息保存到 sessionStorage中
        sessionStorage.setItem('userInfo',JSON.stringify(res.data))
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container{
  background-image: url(../assets/imgs/bg.jpg);
  background-repeat: no-repeat;
  background-size: cover;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-box{
    background: white;
    width: 400px;
    height: 350px;
    border-radius: 6px;
    padding: 50px;
    box-sizing: border-box;
    position: relative;

    .avatar-box{
      width: 120px;
      height: 120px;
      border: 5px solid pink;
      text-align: center;
      border-radius: 120px;
      line-height: 110px;
      position: absolute;
      background-color: #fff;
      top: -60px;
      right: 140px;
    }
    .title{
      position: absolute;
      top: 100px;
      left: 50%;
      // 平移自身长度的50%
      transform: translate(-50%);
      transform: 18px;     
    }
    .login-form{
      position: absolute;
      bottom: 20px;
      left: 50%;
      width: 70%;
      transform: translate(-50%);
      .login-btn{
        width: 100%;
      }
    }
  }
}
</style>